//
//  SISSMortgageAppointmentShowViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/12/27.
//  Copyright © 2018 优房网. All rights reserved.
//

#import "SISSMortgageAppointmentShowViewController.h"
#import "SISSMortgageAppointmentViewController.h"
#import "SISSTitleView.h"
#import "SISSShowView.h"
#import "SISSPassengerProgressViewController.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSComplainCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface SISSMortgageAppointmentShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

/// 预约信息
@property (weak, nonatomic) SISSTitleView *reserveTitleView;
@property (weak, nonatomic) SISSShowView *servicePointShowView;
@property (weak, nonatomic) SISSShowView *timeShowView;
@property (weak, nonatomic) SISSShowView *addressShowView;

///
@property (weak, nonatomic) SISSTitleView *attachmentTitleView;
@property (weak, nonatomic) UICollectionView *attachmentCollectionView;

///
@property (weak, nonatomic) UIButton *editButton;
@property (strong, nonatomic) SISSMortgageAppointmentInfoModel *mortgageAppointmentInfoModel;
/// 照片数组
@property (copy ,nonatomic) NSArray *images;
/// 图片url数组
@property (copy ,nonatomic) NSArray *imagesUrlArr;

@end

@implementation SISSMortgageAppointmentShowViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"预约按揭详情";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    [self requestData];
}

/**
 返回按钮点击事件
 */
- (void)backItemAction {
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSPassengerProgressViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)requestData {
    
    [kNetworkManager requestMortgageAppointmentInfoWithAdbpId:self.adbpId success:^(SISSMortgageAppointmentInfoModel *mortgageAppointmentInfoModel) {
        self.mortgageAppointmentInfoModel = mortgageAppointmentInfoModel;
        if (self.isHistory && !mortgageAppointmentInfoModel) {
            for (UIView *view in self.view.subviews) {
                view.hidden = YES;
            }
        }
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)setMortgageAppointmentInfoModel:(SISSMortgageAppointmentInfoModel *)mortgageAppointmentInfoModel {
    _mortgageAppointmentInfoModel = mortgageAppointmentInfoModel;
    self.servicePointShowView.rightStr = mortgageAppointmentInfoModel.bank;
    self.timeShowView.rightStr = mortgageAppointmentInfoModel.applyTime;
    self.addressShowView.rightStr = mortgageAppointmentInfoModel.address;
    
    self.imagesUrlArr = mortgageAppointmentInfoModel.image;
    
    CGFloat itemW = (SCREEN_WIDTH - 3 * 1) / 3;
    CGFloat collectViewHeight = self.imagesUrlArr.count > 6 ? itemW * 3 : (self.imagesUrlArr.count > 3 ? itemW * 2 : itemW);
    [self.attachmentCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(collectViewHeight);
    }];
    [self.attachmentCollectionView reloadData];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSComplainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    
    if (self.images.count > 0) {
        cell.imageView.image = self.images[indexPath.row];
    } else {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlArr[indexPath.item]]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

// 编辑按钮点击事件
- (void)editButtonClickAction {
    SISSMortgageAppointmentViewController *vc = [[SISSMortgageAppointmentViewController alloc] init];
    vc.adbpId = self.adbpId;
    vc.mortgageAppointmentInfoModel = self.mortgageAppointmentInfoModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTitleView *reserveTitleView = [[SISSTitleView alloc] init];
    reserveTitleView.titleStr = @"按揭办理银行";
    [self.containerView addSubview:reserveTitleView];
    self.reserveTitleView = reserveTitleView;
    
    SISSShowView *servicePointShowView = [[SISSShowView alloc] init];
    servicePointShowView.leftStr = @"银行名称：";
    servicePointShowView.isShowBottomLine = YES;
    [self.containerView addSubview:servicePointShowView];
    self.servicePointShowView = servicePointShowView;
    
    SISSShowView *timeShowView = [[SISSShowView alloc] init];
    timeShowView.leftStr = @"预约时间：";
    timeShowView.isShowBottomLine = YES;
    [self.containerView addSubview:timeShowView];
    self.timeShowView = timeShowView;
    
    SISSShowView *addressShowView = [[SISSShowView alloc] init];
    addressShowView.leftStr = @"取证地点：";
    addressShowView.isShowBottomLine = YES;
    [self.containerView addSubview:addressShowView];
    self.addressShowView = addressShowView;
    
    SISSTitleView *attachmentTitleView = [[SISSTitleView alloc] init];
    attachmentTitleView.titleStr = @"按揭审批表上传";
    [self.containerView addSubview:attachmentTitleView];
    self.attachmentTitleView = attachmentTitleView;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    CGFloat itemW = (SCREEN_WIDTH - 3 * 10) / 3;
    flowLayout.itemSize = CGSizeMake(itemW, itemW);
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 10, 1, 10);
    UICollectionView *attachmentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    attachmentCollectionView.backgroundColor = [UIColor whiteColor];
    attachmentCollectionView.dataSource = self;
    attachmentCollectionView.delegate = self;
    [attachmentCollectionView registerNib:[UINib nibWithNibName:@"SISSComplainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ID"];
    [self.containerView addSubview:attachmentCollectionView];
    self.attachmentCollectionView = attachmentCollectionView;
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editButton setBackgroundColor:MAIN_COLOR];
    [editButton addTarget:self action:@selector(editButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:editButton];
    self.editButton = editButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf;
    [self.reserveTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.servicePointShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.reserveTitleView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.timeShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.servicePointShowView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.addressShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.timeShowView.mas_bottom);
        make.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.attachmentTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressShowView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.attachmentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.attachmentTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(8 * globelMargin);
        make.bottom.equalTo(weakSelf.containerView).offset(-7 * globelMargin);
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.mas_bottomLayoutGuideTop);
        make.centerX.equalTo(weakSelf.view);
        make.width.mas_equalTo(0.8 * SCREEN_WIDTH);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
}


@end
